#include <QKeyEvent>
#include <QFile>
#include <QCoreApplication>
#include <QMessageBox>
#include <QDebug>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dlg_addgood.h"
#include "dlg_adduser.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
  ,m_ptrSupSql(nullptr)
{
    ui->setupUi(this);
    m_dlgLogin.show();
    auto f =[&](QString str){
        this->show();
        ui->label_admin->setText(str);
        QStringList list = ui->label_admin->text().split(":");
        if(list[0] == "user")
        {
            ui->btnSwitch->setEnabled(false);
        }
    };
    connect(&m_dlgLogin,&Page_Login::sendLoginSuccess,this,f);

    auto f2 =[&](){
         reg.show();
     };
     //连接登录窗口的信号与注册窗口的槽函数
     //跳转用户注册界面
     connect(&m_dlgLogin,&Page_Login::enter_register,&reg,f2);

    ui->treeWidget->clear();
    ui->treeWidget->setColumnCount(1);
    QStringList l;
    l<<"超市库存管理";

    QTreeWidgetItem *pf = new QTreeWidgetItem(ui->treeWidget,l);
    ui->treeWidget->addTopLevelItem(pf);
    QString str1 = ui->label_admin->text();
        l.clear();
        l<<"员工管理";
        QTreeWidgetItem *p1 = new QTreeWidgetItem(pf,l);
        l.clear();
        l<<"管理员管理";
        QTreeWidgetItem *p2 = new QTreeWidgetItem(pf,l);
        pf->addChild(p1);
        pf->addChild(p2);

    ui->treeWidget->expandAll();
    ui->stackedWidget->setCurrentIndex(0);
    m_ptrSupSql = supsql::getinstance();
    m_ptrSupSql->init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_btn_Exit_clicked()
{
    exit(0);
}

void MainWindow::on_btnShowAllGoods_clicked()
{
    UpDateTable();
}

void MainWindow::on_btnAdd_clicked()
{
    m_dlgAddGood.setType(true);
    m_dlgAddGood.exec();//模态对话框，阻止其他对话框的拖动和使用，必须先完成这个对话框的内容
    UpDateTable();
}

void MainWindow::UpDateTable()
{
    ui->tableWidget->verticalHeader()->hide();//取消行号
    ui->tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40); //设置行高
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //均分各列

    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(6);
    QStringList l;
    l<<"序号"<<"id"<<"商品名"<<"种类"<<"价格"<<"数量";
    ui->tableWidget->setHorizontalHeaderLabels(l);
    ui->tableWidget->setSelectionBehavior(QTableWidget::SelectRows);//只选中行
    ui->tableWidget->setSelectionMode(QTableWidget::SingleSelection);//取消同时选择多行
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    auto cnt =m_ptrSupSql->getGoodCnt();
    QList<GoodInfo> lGoods = m_ptrSupSql->getPageGood(0,cnt);

    ui->tableWidget->setRowCount(cnt);
    for(int i=0;i<lGoods.size();i++)
    {
        if(true)
        {
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(i+1)));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(QString::number(lGoods[i].id)));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(lGoods[i].name));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(lGoods[i].kind));
        ui->tableWidget->setItem(i,4,new QTableWidgetItem(QString::number(lGoods[i].price,'f',2)));
        ui->tableWidget->setItem(i,5,new QTableWidgetItem(QString::number(lGoods[i].num)));
        }
    }
    for (int i=0;i<cnt;i++)
           {
               for (int j=0;j<6;j++)
               {
                   ui->tableWidget->item(i,j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
               }
           }
    ui->label_count->setText(QString("数量:%1").arg(cnt));
}

void MainWindow::UpDateTable2()
{
    ui->tableWidget2->verticalHeader()->hide();//取消行号
    ui->tableWidget2->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
    ui->tableWidget2->verticalHeader()->setDefaultSectionSize(40); //设置行高
    ui->tableWidget2->horizontalHeader()->setStretchLastSection(true); //均分各列
    ui->tableWidget2->clear();
    ui->tableWidget2->setColumnCount(6);
    QStringList l;
    l<<"序号"<<"id"<<"姓名"<<"用户名"<<"地址"<<"电话";
    ui->tableWidget2->setHorizontalHeaderLabels(l);
    ui->tableWidget2->setSelectionBehavior(QTableWidget::SelectRows);//只选中行
    ui->tableWidget2->setSelectionMode(QTableWidget::SingleSelection);//取消同时选择多行
    ui->tableWidget2->setEditTriggers(QAbstractItemView::NoEditTriggers);
    auto cnt =m_ptrSupSql->getUserCnt();
    QList<UserInfo> lUsers = m_ptrSupSql->getPageUser(0,cnt);

    ui->tableWidget2->setRowCount(cnt);
    QString str1 = ui->EditSearch->text();
    for(int i=0;i<lUsers.size();i++)
    {
            ui->tableWidget2->setItem(i,0,new QTableWidgetItem(QString::number(i+1)));
            ui->tableWidget2->setItem(i,1,new QTableWidgetItem(QString::number(lUsers[i].id)));
            ui->tableWidget2->setItem(i,2,new QTableWidgetItem(lUsers[i].name));
            ui->tableWidget2->setItem(i,3,new QTableWidgetItem(QString::number(lUsers[i].username)));
            ui->tableWidget2->setItem(i,4,new QTableWidgetItem(lUsers[i].address));
            ui->tableWidget2->setItem(i,5,new QTableWidgetItem(lUsers[i].phone));
            ui->tableWidget2->setItem(i,6,new QTableWidgetItem(lUsers[i].Aur));

    }
    for (int i=0;i<cnt;i++)
           {
               for (int j=0;j<6;j++)
               {
                   ui->tableWidget2->item(i,j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
               }
           }
    ui->label_count->setText(QString("数量:%1").arg(cnt));
    ui->label_count2->setText(QString("数量:%1").arg(cnt));
}


void MainWindow::on_btnRemove_clicked()
{
    int i = ui->tableWidget->currentRow();
    if(i>=0)
    {
        int id = ui->tableWidget->item(i,1)->text().toInt();
        m_ptrSupSql->RemoveGood(id);
        QMessageBox::information(nullptr,"信息","删除成功");

    }
    UpDateTable();
}

void MainWindow::on_btnChange_clicked()
{
    GoodInfo info;
    int i = ui->tableWidget->currentRow();
    if(i>=0)
    {
        info.id = ui->tableWidget->item(i,1)->text().toInt();
        info.name = ui->tableWidget->item(i,2)->text();
        info.kind = ui->tableWidget->item(i,3)->text();
        info.price = ui->tableWidget->item(i,4)->text().toFloat();
        info.num = ui->tableWidget->item(i,5)->text().toInt();
        m_dlgAddGood.setType(false,info);
        m_dlgAddGood.exec();

    }
    UpDateTable();


}

void MainWindow::on_btnSearch_clicked()
{
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(6);
    QStringList l;
    l<<"序号"<<"id"<<"商品名"<<"种类"<<"价格"<<"数量";
    ui->tableWidget->setHorizontalHeaderLabels(l);
    ui->tableWidget->setSelectionBehavior(QTableWidget::SelectRows);//只选中行
    ui->tableWidget->setSelectionMode(QTableWidget::SingleSelection);//取消同时选择多行
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    auto cnt =m_ptrSupSql->getGoodCnt();
    QString str = ui->EditSearch->text();
    if(str.isEmpty())
    {
        QMessageBox::information(nullptr,"警告","搜索栏为空");
        return;
    }
    QList<GoodInfo> lGoods = m_ptrSupSql->getPageGood(0,cnt);
    int index =0;

    for(int i=0;i<lGoods.size();i++)
    {   if(lGoods[i].name.contains(str) ||lGoods[i].kind.contains(str))
        {
            ui->tableWidget->setItem(index,0,new QTableWidgetItem(QString::number(index+1)));
            ui->tableWidget->setItem(index,1,new QTableWidgetItem(QString::number(lGoods[i].id)));
            ui->tableWidget->setItem(index,2,new QTableWidgetItem(lGoods[i].name));
            ui->tableWidget->setItem(index,3,new QTableWidgetItem(lGoods[i].kind));
            ui->tableWidget->setItem(index,4,new QTableWidgetItem(QString::number(lGoods[i].price,'f',2)));
            ui->tableWidget->setItem(index,5,new QTableWidgetItem(QString::number(lGoods[i].num)));
            index++;
        }
    }
    ui->tableWidget->setRowCount(index);
    ui->label_count->setText(QString("数量:%1").arg(index));
}


void MainWindow::on_btnAdd_w_clicked()
{
    w_dlgAddUser.setwType(true);
    w_dlgAddUser.exec();//模态对话框，阻止其他对话框的拖动和使用，必须先完成这个对话框的内容
    UpDateTable2();
}



void MainWindow::on_btnSwitch_clicked()
{
    int nCount = ui->stackedWidget->count();
     int nIndex = ui->stackedWidget->currentIndex();

     // 获取下一个需要显示的页面索引
     ++nIndex;

     // 当需要显示的页面索引大于等于总页面时，切换至首页
     if (nIndex >= nCount)
         nIndex = 0;

     ui->stackedWidget->setCurrentIndex(nIndex);
}

void MainWindow::on_btnShowAllWorkers_clicked()
{
    UpDateTable2();
}

void MainWindow::on_btnRemove_w_clicked()
{
    int i = ui->tableWidget2->currentRow();
    qDebug()<<i;
    if(i>=0)
    {
        int username = ui->tableWidget2->item(i,3)->text().toInt();
        qDebug()<<username;

        qDebug()<<m_ptrSupSql->RemoveUser(username);
        QMessageBox::information(nullptr,"信息","删除成功");

    }
    UpDateTable2();
}

void MainWindow::on_btnChange_w_clicked()
{
    UserInfo info;
    int i = ui->tableWidget2->currentRow();
    if(i>=0)
    {
        info.id = ui->tableWidget2->item(i,1)->text().toInt();
        info.name = ui->tableWidget2->item(i,2)->text();
        info.username = ui->tableWidget2->item(i,3)->text().toInt();
        info.address=ui->tableWidget2->item(i,4)->text();
        info.phone = ui->tableWidget2->item(i,5)->text();
        w_dlgAddUser.setwType(false,info);
        w_dlgAddUser.exec();


    }
    UpDateTable2();
}

void MainWindow::on_btnSearch_w_clicked()
{
    ui->tableWidget2->clear();
    ui->tableWidget2->setColumnCount(7);
    QStringList l;
    l<<"序号"<<"id"<<"姓名"<<"用户名"<<"地址"<<"电话"<<"权限";
    ui->tableWidget2->setHorizontalHeaderLabels(l);
    ui->tableWidget2->setSelectionBehavior(QTableWidget::SelectRows);//只选中行
    ui->tableWidget2->setSelectionMode(QTableWidget::SingleSelection);//取消同时选择多行
    ui->tableWidget2->setEditTriggers(QAbstractItemView::NoEditTriggers);
    auto cnt =m_ptrSupSql->getUserCnt();
    QString str = ui->EditSearch_w->text();
    if(str.isEmpty())
    {
        QMessageBox::information(nullptr,"警告","搜索栏为空");
        return;
    }
    QList<UserInfo> lUsers = m_ptrSupSql->getPageUser(0,cnt);
    int index =0;

    for(int i=0;i<lUsers.size();i++)
    {
        if(lUsers[i].name.contains(str))
        {
            ui->tableWidget2->setItem(index,0,new QTableWidgetItem(QString::number(index+1)));
            ui->tableWidget2->setItem(index,1,new QTableWidgetItem(QString::number(lUsers[i].id)));
            ui->tableWidget2->setItem(index,2,new QTableWidgetItem(lUsers[i].name));
            ui->tableWidget2->setItem(index,3,new QTableWidgetItem(QString::number(lUsers[i].username)));
            ui->tableWidget2->setItem(index,4,new QTableWidgetItem(lUsers[i].address));
            ui->tableWidget2->setItem(index,5,new QTableWidgetItem(lUsers[i].phone));
            index++;
        }
    }
    ui->tableWidget->setRowCount(index);
    ui->label_count2->setText(QString("数量:%1").arg(index));
}



